package com.xsb.mvvmlibrary.network.interceptor

import com.xsb.mvvmlibrary.network.UrlManager
import okhttp3.HttpUrl
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException


/**
 *     author : 薛少博
 *     e-mail : xsb@zlhopesun.com
 *     time   : 2021/5/20
 *     desc   : 更换baseUrl
 */

class BaseUrlInterceptor : Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        // 获取request
        val request: Request = chain.request()
        // 从request中获取原有的HttpUrl实例oldHttpUrl
        val oldHttpUrl: HttpUrl = request.url()
        // 获取request的创建者builder
        val builder: Request.Builder = request.newBuilder()
        // 从request中获取headers，通过给定的键url_name
        val headerValues: List<String> = request.headers("url_name")
        if (headerValues != null && headerValues.size > 0) {
            // 如果有这个header，先将配置的header删除，因此header仅用作app和okhttp之间使用
            builder.removeHeader("url_name")
            // 匹配获得新的BaseUrl
            val headerValue = headerValues[0]
            var newBaseUrl: HttpUrl? = null
            newBaseUrl = if ("base_one" == headerValue) {
                HttpUrl.parse(UrlManager.SERVER_URL)
            } else if ("base_two" == headerValue) {
                HttpUrl.parse(UrlManager.SERVER_URL1)
            } else {
                oldHttpUrl
            }
            // 重建新的HttpUrl，修改需要修改的url部分
            val newFullUrl = oldHttpUrl
                .newBuilder() // 更换网络协议
                .scheme(newBaseUrl!!.scheme()) // 更换主机名
                .host(newBaseUrl.host()) // 更换端口
                .port(newBaseUrl.port())
                .build()
            // 重建这个request，通过builder.url(newFullUrl).build()；
            // 然后返回一个response至此结束修改
            return chain.proceed(builder.url(newFullUrl).build())
        }
        return chain.proceed(request)
    }
}